package com.th.aliyun.oss.config;

import com.aliyun.oss.OSSClient;
import com.th.aliyun.oss.service.impl.ApiBootOssService;
import com.th.aliyun.oss.process.AliYunOssObjectStorageProgress;
import com.th.aliyun.oss.properties.OssProperties;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * 整合阿里云Oss对象存储自动化配置
 *
 * @author：恒宇少年 - 于起宇
 */
@Configuration
@EnableConfigurationProperties(OssProperties.class)
@ConditionalOnClass(OSSClient.class)
@ConditionalOnProperty(prefix = OssProperties.API_BOOT_OSS_PREFIX, name = {"region", "access-key-id", "access-key-secret", "bucket-name"})
public class OssAutoConfiguration {
    /**
     * ApiBoot Oss 属性配置
     */
    private OssProperties ossProperties;
    /**
     * ApiBoot Progress Provider
     */
    private AliYunOssObjectStorageProgress progress;

    public OssAutoConfiguration(OssProperties ossProperties, ObjectProvider<AliYunOssObjectStorageProgress> apiBootProgressProvider) {
        this.ossProperties = ossProperties;
        this.progress = apiBootProgressProvider.getIfAvailable();
    }

    /**
     * 实例化ApiBootOssService
     *
     * @return ApiBootOssService 实例
     */
    @Bean
    @ConditionalOnMissingBean
    ApiBootOssService apiBootOssService() {
        ApiBootOssService apiBootOssService = new ApiBootOssService(ossProperties);
        apiBootOssService.setApiBootObjectStorageProgress(progress);
        return apiBootOssService;
    }
}
